package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.dto.CasePendingFormDto;
import cn.livingCloud.dto.JsonDto;
import cn.livingCloud.entity.core.CaseRecord;
import cn.livingCloud.entity.core.CaseRecordType;
import cn.livingCloud.entity.core.CaseStatus;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.repository.CaseImageMapper;
import cn.livingCloud.repository.CaseRecordMapper;

import java.util.List;

/**
 * 案件审批
 * Created by 唐旭 on 2016/6/21.
 */
public class CasePendingFormDtoUpdator extends CaseOperator {
    protected transient CaseImageMapper caseImageMapper = BeanProvider.getBean(CaseImageMapper.class);
    protected transient CaseRecordMapper recordMapper = BeanProvider.getBean(CaseRecordMapper.class);
    private CasePendingFormDto formDto;

    public CasePendingFormDtoUpdator(CasePendingFormDto formDto) {
        super(formDto);
        this.formDto = formDto;
    }

    @Override
    protected JsonDto operation() {
        if (formDto.isAllow()) {
            super.aCase.setStatus(CaseStatus.FINISH);
        } else {
            caseImageMapper.deleteByCaseUuid(formDto.getCaseUuid());
//            super.aCase.setStatus(CaseStatus.NO_HANDLE);
            List<CaseRecord> records = recordMapper.findCaseRecord(aCase.getUuid(), CaseRecordType.REJECT_PENDING);
            if (!records.isEmpty() && records.size() > 2) {
                super.aCase.setStatus(CaseStatus.LOCK);
            } else {
                super.aCase.setStatus(CaseStatus.NO_HANDLE);
            }
        }
        msgService.sendPendingMsg(aCase, formDto.getContent());
        JsonDto jsonDto = new JsonDto();
        jsonDto.setState(true);
        return jsonDto;
    }

    @Override
    protected List<Employee> getNewOwners() {
        return null;
    }
}
